<template>
  <div class="dashboard-container">
    <h1 style="text-align: center">住房管理</h1>

    <el-dialog title="入住" :visible.sync="addButtonController" width="30%" @close="closeStay('ruleForm')">
      <el-form label-width="20%" :model="addList" ref="ruleForm">
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="addList.roomNumber" maxlength="11" show-word-limit style="width: 80%" clearable
                    placeholder="请输入房间号"
          />
        </el-form-item>

        <el-form-item label="顾客" prop="customerId">
          <el-select v-model="addList.customerId" clearable placeholder="顾客" style="width: 80%">
            <el-option
              v-for="item in customerList"
              :key="item.customerId"
              :label="item.customerName + '-' + item.identificationCard"
              :value="item.customerId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addList.remark" maxlength="100" show-word-limit style="width: 80%" clearable
                    placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" plain @click="insertStay('ruleForm')">确 定</el-button>
        <el-button plain @click="closeStay('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="退房" :visible.sync="cancelStayButtonController" width="30%" @close="closeCancelStay('ruleForm')">
      <div style="margin: 10px"><span>请确认是否退房</span></div>
      <span slot="footer" class="dialog-footer">
          <el-button type="info" plain @click="cancelStay('ruleForm')">确定</el-button>
          <el-button plain @click="closeCancelStay('ruleForm')">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="作废" :visible.sync="abrogateStayButtonController" width="30%"
               @close="closeAbrogateStay('ruleForm')"
    >
      <div style="margin: 10px"><span>请确认是否作废</span></div>
      <span slot="footer" class="dialog-footer">
          <el-button type="info" plain @click="abrogateStay('ruleForm')">确定</el-button>
          <el-button plain @click="closeAbrogateStay('ruleForm')">取消</el-button>
      </span>
    </el-dialog>

    <el-select v-model="status" clearable placeholder="房间状态" style="margin-left: 30%;width: 200px">
      <el-option
        v-for="item in stayOptions"
        :key="item.status"
        :label="item.status"
        :value="item.status"
      >
      </el-option>
    </el-select>

    <el-input
      v-model="roomNumber"
      placeholder="房间号"
      name="roomNumber"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />


    <el-button plain
               style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: #cfefc6; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
               @click="selectAll"
    >
      查询
    </el-button>
    <el-button
      style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
      @click="checkStay"
    >重置
    </el-button>
    <el-table :data="stayList.slice((currentPage-1)*pagesize,currentPage*pagesize)" height="659px" border style="width: 100%;margin-top: 20px"
              class="elegant-room-table"
    >
      <el-table-column align="center" fixed prop="roomId" label="房间ID" min-width="100"/>
      <el-table-column align="center" fixed prop="roomNumber" label="房间号" min-width="100"/>
      <el-table-column align="center" fixed prop="status" label="房间状态" min-width="150">
        <template v-slot="scope">
    <span :class="{
        'status-green': scope.row.status === '空闲',
        'status-red': scope.row.status === '已入住'
      }"
    >
      {{ scope.row.status }}
    </span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed prop="remark" label="房间备注" min-width="150"/>
      <el-table-column align="center" fixed prop="roomTypeName" label="房间类型" width="150"/>
      <el-table-column align="center" fixed prop="roomPrice" label="房间价格" width="130">
        <template v-slot="scope">
          <span class="price-tag">¥{{ scope.row.roomPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed prop="customerName" label="顾客名称" width="130"/>
      <el-table-column align="center" fixed prop="identificationCard" label="身份证号" width="200"/>
      <el-table-column align="center" fixed label="操作" width="150">
        <template v-slot="scope">
          <el-button type="text" v-if="scope.row.status === '已入住'" @click="cancelStayButton(scope.row)">退房
          </el-button>
          <el-button type="text" v-if="scope.row.status === '已入住'" @click="abrogateStayButton(scope.row)">作废
          </el-button>
          <el-button type="text" v-if="scope.row.status === '空闲'" @click="addButton(scope.row)">入住</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:5px;margin-left: 30%;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,8,10,15,20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="stayList.length">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteRoomType, insert1RoomType, selectAllRoomType, updateRoomType } from '@/api/roomType'
import { insertCustomer, selectAllCustomer, updateCustomer } from '@/api/customer'
import { selectAllLiveRecords } from '@/api/liveRecords'
import { abrogateStay1, cancelStay, insertStay, selectAllStay1 } from '@/api/stay'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data() {
    return {
      status: '',
      roomNumber: '',
      stayList: [],
      stayOptions: [], // 下拉
      customerList: [],
      addButtonController: false,
      cancelStayButtonController: false,
      abrogateStayButtonController: false,
      abrogateStayLiveTableId: '',
      abrogateStayLiveId: '',
      currentPage: 1,
      currentIndex: '',
      pagesize: 10,
      addList: {
        customerId: '',
        roomNumber: '',
        customerName: '',
        identificationCard: '',
        remark: '',
        roomPrice: ''
      },
      liveTableId: ''
    }
  },
  mounted() {
    this.selectAll()
    this.fetchStay()
    this.fetchCustomerList()
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage= val
    },
    selectAll() {
      selectAllStay1({
        status: this.status,
        roomNumber: this.roomNumber
      }).then(
        response => {
          console.log(response.data)
          this.stayList = response.data
          if((this.stayList.slice((this.currentPage-1)*this.pagesize,this.currentPage*this.pagesize)).length == 0){
            this.currentPage = 1
          }
        }
      )
    },
    insertStay(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertStay({
            customerId: this.addList.customerId,
            roomNumber: this.addList.roomNumber,
            roomId: this.addList.roomId,
            customerName: this.addList.customerName,
            identificationCard: this.addList.identificationCard,
            remark: this.addList.remark,
            roomPrice: this.addList.roomPrice
          }).then(
            async response => {
              this.addButtonController = false
              this.$message.success('入住成功')
              this.selectAll()
            }
          )
        } else {
          return false
        }
      })
    },
    cancelStay() {
      cancelStay({
        liveTableId: this.liveTableId
      }).then(response => {
          this.selectAll()
          this.$message.success('退房成功')
          this.cancelStayButtonController = false
        }
      )
    },
    abrogateStay(formName) {
      abrogateStay1({
        liveTableId: this.abrogateStayLiveTableId
        , liveId: this.abrogateStayLiveId
      }).then(
        async response => {
          this.abrogateStayButtonController = false
          this.$message.success('作废成功')
          this.selectAll()
        })
    },
    // 下拉
    fetchStay() {
      selectAllStay1().then(response => {
        this.stayOptions = Object.values(
          response.data.reduce((selectStatus, item) => {
            selectStatus[item.status] = item
            return selectStatus
          }, {})
        )
      })
    },
    fetchCustomerList() {
      selectAllCustomer().then(response => {
        this.customerList = response.data
      })
    },
    addButton(row) {
      this.addList = {
        customerId: '',
        roomNumber: row.roomNumber,
        roomId: row.roomId,
        roomTypeName: row.roomTypeName,
        customerName: '',
        identificationCard: '',
        remark: '',
        roomPrice: row.roomPrice
      }
      this.addButtonController = true
    },
    cancelStayButton(row) {
      this.liveTableId = row.liveTableId
      this.cancelStayButtonController = true
    },
    abrogateStayButton(row) {
      this.abrogateStayLiveTableId = row.liveTableId
      this.abrogateStayLiveId = row.liveId
      this.abrogateStayButtonController = true
    },
    checkStay() {
      this.status = ''
      this.roomNumber = ''
      this.selectAll()
    },
    closeStay(formName) {
      this.$refs[formName].resetFields()
      this.addButtonController = false
      this.cancelStayButtonController = false
    },

    closeCancelStay() {
      this.cancelStayButtonController = false
    },

    closeAbrogateStay() {
      this.abrogateStayButtonController = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.elegant-room-table {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  overflow: hidden;

  &::before {
    display: none;
  }

  .price-tag {
    display: inline-block;
    padding: 3px 8px;
    color: #171717;
    border-radius: 12px;
    font-weight: 500;
  }
}
.status-green {
  color: green;
}

.status-red {
  color: red;
}
</style>
